<!DOCTYPE html>
<head>
<script src="../resources/js-test.js"></script>
<script src="resources/netinfo_common.js"></script>
</head>
<body>
<script>

description('Tests using NetInfo from multiple frames.');

shouldBe('typeof internals.observeGC', '"function"',
'this test requires window.internals');

var childFrame = document.createElement('iframe');
document.body.appendChild(childFrame);

var childConnection = childFrame.contentWindow.navigator.connection;

if (connection.type != childConnection.type)
    testFailed("Connection type not the same between main frame and child.");

var hasMainFrameEventFired = false;
var hasChildFrameEventFired = false;

function mainFrameListener() {
    hasMainFrameEventFired = true;
    if (connection.type != newConnectionType)
        testFailed("Event fired but type not yet changed.");
    if (connection.downlinkMax != newDownlinkMax)
        testFailed("Event fired but downlinkMax not yet changed.");
    if (!hasChildFrameEventFired && childConnection.type != initialType)
        testFailed("Child frame connection type changed before firing its event.");
    maybeFinishTest();
}

function childFrameListener() {
    hasChildFrameEventFired = true;
    if (childConnection.type != newConnectionType)
        testFailed("Child frame fired event but type not yet changed.");
    if (childConnection.downlinkMax != newDownlinkMax)
        testFailed("Child frame fired event but downlinkMax not yet changed.");
    if (!hasMainFrameEventFired && connection.type != initialType)
        testFailed("Main frame connection type changed before firing its event.");
    maybeFinishTest();
}

function maybeFinishTest() {
    if (hasMainFrameEventFired && hasChildFrameEventFired) {
        finishJSTest();
    }
}

connection.addEventListener('change', mainFrameListener);
childConnection.addEventListener('change', childFrameListener);

internals.setNetworkConnectionInfoOverride(isTypeOnline(newConnectionType), newConnectionType, initialEffectiveType, initialRtt, newDownlinkMax);

</script>
</body>
</html>
